﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using SoulHackerCUnite.Data;
using SoulHackerCUnite.DataAdapter;
using SoulHackerCUnite.Functions;
using SoulHackerCUnite.Extensions;

namespace TestProject
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private List<Demon> _demons;
        private List<UniteRule> _rules;
        private List<Race> _races;
        private Unite _unite = new Unite();

        private List<ResultToShow> _resultsToShow;

        public MainWindow()
        {
            InitializeComponent();
            LoadDatas();
        }

        private void LoadDatas()
        {
            SqliteAdapter adapter = new SqliteAdapter();
            adapter.DataFile = ".\\soulhackersc.db3";
            adapter.Open();

            _demons = adapter.FillDemons();
            _rules = adapter.FillRules();
            _races = adapter.FillRaces();

            _unite.Skills = adapter.FillSkills();
            _unite.SkillInherts = adapter.FillSkillInhert();

            adapter.Close();

            _unite.Demons = _demons;
            _unite.Rules = _rules;
            _unite.Races = _races;

            _resultsToShow = new List<ResultToShow>();

            ////dgDemons.AutoGenerateColumns = true;
            dgDemons.ItemsSource = _demons;
            
        }

        private void bUnite_Click(object sender, RoutedEventArgs e)
        {
       
            if(dgDemons.SelectedItems.Count==2) // twin unite
            {
                Demon demon1 = dgDemons.SelectedItems[0] as Demon;
                Demon demon2 = dgDemons.SelectedItems[1] as Demon;

                Demon result = _unite.Unite2(demon1, demon2);
                if (result == null)
                    MessageBox.Show("Can not unite this two");
                else
                {
                    ResultToShow resultToShow = new ResultToShow() { Demon1 = demon1, Demon2 = demon2, Result = result};
                    _resultsToShow.Clear();
                    _resultsToShow.Add(resultToShow);
                    //dgResults.Items.Clear();
                    //dgResults.Items.Add(resultToShow);
                    ////MessageBox.Show(result.Level + " " + result.RaceJ + " " + result.Name);
                    dgResult.Items.Clear();
                    dgResult.Items.Add(result);
                }
            }

            else if (dgDemons.SelectedItems.Count == 3) // triple unite
            {
                Demon demon1 = dgDemons.SelectedItems[0] as Demon;
                Demon demon2 = dgDemons.SelectedItems[1] as Demon;
                Demon demon3 = dgDemons.SelectedItems[2] as Demon;

                Demon result = _unite.Unite3(demon1, demon2,demon3);
                if (result == null)
                    MessageBox.Show("Can not unite this three");
                else
                {
                    ResultToShow resultToShow = new ResultToShow() { Demon1 = demon1,Demon2 = demon2,Demon3 = demon3,Result = result,Plus2 = "+"};
                    _resultsToShow.Clear();
                    _resultsToShow.Add(resultToShow);
                    //dgResults.Items.Clear();
                    //dgResults.Items.Add(resultToShow);
                    ////MessageBox.Show(result.ToString());
                    dgResult.Items.Clear();
                    dgResult.Items.Add(result);
                }
            }
            else if (dgDemons.SelectedItems.Count > 3) // triple unite
            {
                List<Demon> demons = new List<Demon>();
                foreach (var selectedItem in dgDemons.SelectedItems)
                {
                    demons.Add(selectedItem as Demon);
                }
                _resultsToShow = _unite.UniteMuti(demons);
                if(_resultsToShow!=null)
                {
                    demons = new List<Demon>();
                    foreach (ResultToShow toShow in _resultsToShow)
                    {
                        demons.CheckAdd(toShow.Result);
                    }
                    dgResult.Items.Clear();
                    foreach (Demon demon in demons)
                    {
                        dgResult.Items.Add(demon);
                    }
                }
            }
        }

        private void dgResult_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (e.Source is DataGridRow)
            {
                DataGridRow row = e.Source as DataGridRow;

                Demon result = row.Item as Demon;
                if (result != null)
                {
                    ////dgResults.Items.Clear();
                    dgResults.ItemsSource = _resultsToShow.FindAll(r => r.Result.Name == result.Name);
                }
            }
        }

        private void dgDemons_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (e.Source is DataGridRow)
            {
                DataGridRow row = e.Source as DataGridRow;

                Demon demon = row.Item as Demon;
                if (demon != null)
                {
                    DemonInfo info = new DemonInfo(demon);
                    info.Show();
                }
            }
            
        }

        private void bUnunite_Click(object sender, RoutedEventArgs e)
        {
            /*SqliteAdapter adapter = new SqliteAdapter();
            adapter.DataFile = ".\\soulhackersc.db3";
            adapter.Open();

            List<ResultToShow> results = _unite.UniteMuti(_demons);
            adapter.SaveUniteToDB(results);

            adapter.Close();*/

            if(dgDemons.SelectedItems.Count>0)
            {
                _resultsToShow.Clear();
                dgResult.Items.Clear();
                
                foreach (object selectedItem in dgDemons.SelectedItems)
                {
                    Demon demon = selectedItem as Demon;
                    if(demon!=null)
                    {
                        List<ResultToShow> ununite2 = _unite.UnUnite2(demon);
                        if (ununite2 != null && ununite2.Count > 0)
                        {
                            _resultsToShow.AddRange(ununite2);
                            //dgResult.Items.Add(demon);
                        }

                        List<ResultToShow> ununite3 = _unite.UnUnite3(demon);
                        if (ununite3 != null && ununite3.Count > 0)
                        {
                            _resultsToShow.AddRange(ununite3);
                            //dgResult.Items.Add(demon);
                        }

                        if((ununite2 != null && ununite2.Count > 0)||(ununite3 != null && ununite3.Count > 0))
                        {
                            dgResult.Items.Add(demon);
                        }
                    }
                }                
                
            }
        }

        private void dgResults_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (e.Source is DataGridRow)
            {
                DataGridRow row = e.Source as DataGridRow;
                ResultToShow toShow = row.Item as ResultToShow;
                if (toShow != null)
                {
                    DemonInfo info = new DemonInfo(toShow.Result);
                    info.Show();
                }
            }
        }
    }
}
